#include <stdbool.h>
#include "parser.h"
#include "utils.h"
#include <stdio.h>

item_t *theItemList;
box_t * theBoxList;
enemy_t *theEnemyList;


void parseItems(item_t *itemList){
    FILE *file;
    file = fopen("data/items.dta", "r");
    int qty = 1;
    if(file){
        char line [ 128 ];
        char seps[]   = ",\n";
        while (fgets(line, sizeof line, file)!= NULL){
            char *config = strtok(line, seps);
            item_t *item = (item_t *)malloc(sizeof(item_t));
            if(!item){
                kill("Error de memoria.");
            }
            item->name = malloc(strlen(config)+1);
            strcpy(item->name, config);
            config = strtok(NULL, seps);
            sscanf(config,"%d", &item->attack);
            config = strtok(NULL, seps);
            sscanf(config,"%d", &item->defense);
            config = strtok(NULL, seps);

            itemList[qty-1] = *item;
            qty++;
            itemList = (item_t *)realloc(itemList, sizeof(item_t)*qty);
            item = NULL;
            free(item);
        }
    }else{
        kill("No se pudo cargar el archivo de configuracion. Vuelva a instalar el juego.");
    }

    theItemList = itemList;
}

item_t getItem(int position){
//    item_t item;
//    return item;
    return theItemList[position];
}

void parseBoxes(box_t *boxList){
    FILE *file;
    file = fopen("data/boxes.dta", "r");
    int qty = 1;
    if(file){
        char line [ 128 ];
        char seps[]   = ",\n";
        while (fgets(line, sizeof line, file)!= NULL){
            char *config = strtok(line, seps);
            box_t *box = (box_t *)malloc(sizeof(box_t));
            if(!box){
                kill("Error de memoria.");
            }

            box->key = booleanFromStr(config);
            config = strtok (NULL, seps);
            int j = 0;
            while (config != NULL){
                box->item[j] = getItem(intFromString(config));
                config = strtok (NULL, seps);
                j++;
            }

            boxList[qty-1] = *box;
            qty++;
            boxList = (box_t *)realloc(boxList, sizeof(box_t)*qty);
            box = NULL;
            free(box);
        }
    }else{
        kill("No se pudo cargar el archivo de Configuracion. Vuelva a instalar el juego.");
    }

    theBoxList = boxList;
}
box_t getBox(int position){
//    box_t box;
//    return box;
    return theBoxList[position];
}

void parseEnemies(enemy_t *enemyList){
    FILE *file;
    file = fopen("data/enemies.dta", "r");
    int qty = 1;
    if(file){
        char line [ 128 ];
        char seps[]   = ",\n";
        while (fgets(line, sizeof line, file)!= NULL){
            char *config = strtok(line, seps);
            enemy_t *enemy = (enemy_t *)malloc(sizeof(enemy_t));
            if(!enemy){
                kill("Error de memoria.");
            }
            enemy->name = malloc(strlen(config)+1);
            strcpy(enemy->name, config);
            config = strtok(NULL, seps);
            sscanf(config,"%d", &enemy->life);
            config = strtok(NULL, seps);
            sscanf(config,"%d", &enemy->powerMax);
            config = strtok(NULL, seps);
            sscanf(config,"%d", &enemy->powerMin);

            enemyList[qty-1] = *enemy;
            qty++;
            enemyList = (enemy_t *)realloc(enemyList, sizeof(enemy_t)*qty);
            enemy = NULL;
            free(enemy);
        }
    }else{
        kill("No se pudo cargar el archivo de Configuracion. Vuelva a instalar el juego.");
    }

    theEnemyList = enemyList;

    fclose(file);
}

enemy_t getEnemy(int position){
//    enemy_t myEnemy;
//    return myEnemy;
    return theEnemyList[position];
}

void parseRooms(int width, int height, room_t *rooms){
    FILE *file;
    file = fopen("data/rooms.dta", "r");
    room_t maze[width][height];
    if (file) {
      char line [ 128 ]; /* or other suitable maximum line size */
      int x;
      int y;
      char seps[]   = ",\n";
      while ( fgets ( line, sizeof line, file ) != NULL ){
        //printf(line);
        //printf("\n");
        char * pch;
        //pch = strtok (line,seps);
        room_t *room = (room_t *)malloc(sizeof(room_t));
        if(!room){
            kill("Error de memoria.");
        }
        pch = strtok(line,seps);
        strcpy(room->here, pch);
        pch = strtok(NULL,seps);
        room->message = malloc(strlen(pch)+1);
        if(!room->message){
            kill("Error de memoria.");
        }
        strcpy(room->message, pch);
        room->block = booleanFromStr(strtok(NULL, seps));
        room->enemy = getEnemy(intFromString(strtok(NULL, seps)));
        room->objects = getBox(intFromString(strtok(NULL, seps)));
        pch = strtok (NULL, seps);
        int j = 0;
        while (pch != NULL){
            room->mov[j] = (char*)malloc(3*sizeof(char));
            if(!room->mov[j]){
                kill("Error de memoria.");
            }
            strcpy(room->mov[j],pch);
            pch = strtok (NULL, seps);
            j++;

        }
        sscanf(room->here, "%d.%d",&x,&y);
        maze[x][y] = *room;
        room = NULL;
        pch = NULL;
        free(room);
        free(pch);
        }
    }else{
        kill("Hubo un problema con el archivo de configuracion. Porfavor vuelva a instalar el juego");
    }
   fclose(file);
}
